#include "dbenv.hxx"
#include "datastore.hxx"
#include "datastore_impl.hxx"
#include "dbt.hxx"


DataStore::DataStore(DbEnv& env) 
: m_env(&env), 
  m_datastoreImpl(NULL) {

}

DataStore::~DataStore() {

    if(m_datastoreImpl) {
        close();
    }
}

DbStatus DataStore::open(const std::string& dsName, 
                         uint32_t cacheSize, 
                         uint32_t indexArrayCapacity, 
                         uint32_t segCapacity, 
                         MAPPINGFUNC* mapFunc,
                         uint32_t flag) {

    m_datastoreImpl = new DataStoreImpl(*m_env->m_envImpl);

    uint32_t newflag = 0;
    newflag |= ( (flag & DATASEGMENT_MEMORY)?DATASEGMENT_MEMORY:DATASEGMENT_IO );
    newflag |= ( (flag & INDEXARRAY_DYNAMIC)?INDEXARRAY_DYNAMIC:INDEXARRAY_STATIC );
    newflag |= (flag & DATASTORE_READONLY);

    if( (flag & INDEXARRAY_DYNAMIC) || (flag & DATASEGMENT_MEMORY))  return
        DbStatus("DataStore::open", DbStatus::DB_NOTSUPPORTED);

    return m_datastoreImpl->open(dsName, 
            cacheSize, 
            indexArrayCapacity,
            segCapacity,
            mapFunc,
            newflag);
}

DbStatus DataStore::close() {

    DbStatus ret;
    if(m_datastoreImpl == NULL) return ret;

    ret = m_datastoreImpl->close();
    if(!ret.isok()) return ret;

    delete m_datastoreImpl;
    m_datastoreImpl = NULL;
}

std::string DataStore::getDsName() {
    return m_datastoreImpl->getDsName();
}

std::string DataStore::getDsPath() {
    return m_datastoreImpl->getDsPath();
}

uint32_t DataStore::getIndexArrayCapacity() {
    return m_indexArrayCapacity; 
}

uint32_t DataStore::getSegCapacity() {
    return m_segmentCapacity; 
}

DbStatus DataStore::flush() {
    return m_datastoreImpl->flush();
}

DbStatus DataStore::addPartition(const std::string& partitionName) {
    return m_datastoreImpl->addPartition(partitionName);
}


DbStatus DataStore::get(Dbt& key, Dbt& value) {
    return m_datastoreImpl->get(key, value); 
}

inline DbStatus DataStore::put(Dbt& key, Dbt& value) {
    return m_datastoreImpl->put(key, value);
}

DbStatus DataStore::del(Dbt& key) {
    return m_datastoreImpl->del(key);
}
